package com.example.bingobox.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.bingobox.R;
import com.example.bingobox.model.State;
import com.example.bingobox.model.State.LocationMark;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LocationMarkAdapter extends BaseExpandableListAdapter {
    private final Context context;
    private final State state;
    private final List<String> cities;
    private final Map<String, List<State.LocationMark>> cityMarks;

    public LocationMarkAdapter(Context context, State state) {
        this.context = context;
        this.state = state;
        this.cityMarks = state.getCityMarks();
        this.cities = new ArrayList<>(cityMarks.keySet());
    }

    @Override
    public int getGroupCount() {
        return cities.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String city = cities.get(groupPosition);
        List<State.LocationMark> marks = cityMarks.get(city);
        return marks != null ? marks.size() : 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return cities.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String city = cities.get(groupPosition);
        List<State.LocationMark> marks = cityMarks.get(city);
        return marks != null ? marks.get(childPosition) : null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_group, parent, false);
        }

        TextView cityName = convertView.findViewById(R.id.city_name);
        String city = cities.get(groupPosition);
        cityName.setText(city);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_location_mark, parent, false);
        }

        String city = cities.get(groupPosition);
        List<State.LocationMark> marks = cityMarks.get(city);
        if (marks != null && childPosition < marks.size()) {
            State.LocationMark mark = marks.get(childPosition);
            
            TextView locationText = convertView.findViewById(R.id.location_text);
            ImageButton btnDelete = convertView.findViewById(R.id.btn_delete);
            
            SpannableStringBuilder builder = new SpannableStringBuilder();
            
            // 添加地址
            String address = mark.getLocation().getAddress() != null ? 
                mark.getLocation().getAddress() : "";
            builder.append(address);
            builder.append("\n");
            
            // 添加名称
            builder.append(mark.getLocation().getName());
            builder.append("\n");
            
            // 添加标签
            builder.append(mark.getType());
            
            // 设置样式
            builder.setSpan(
                new ForegroundColorSpan(Color.GRAY),
                0,
                address.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            );
            builder.setSpan(
                new RelativeSizeSpan(0.8f),
                0,
                address.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            );
            
            int tagStart = builder.length() - mark.getType().length();
            builder.setSpan(
                new ForegroundColorSpan(Color.BLUE),
                tagStart,
                builder.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            );
            
            locationText.setText(builder);
            
            // 添加删除按钮的点击事件
            btnDelete.setOnClickListener(v -> {
                marks.remove(childPosition);
                notifyDataSetChanged();
            });
            
            // 确保删除按钮可见
            btnDelete.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
} 